﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Td.Additional.Log;

namespace Td.Additional
{
    /// <summary>
    /// Debug facilities.
    /// </summary>
    public static class Debug
    {
        /// <summary>
        /// Gets or sets the log.
        /// </summary>
        /// <value>The log.</value>
        public static ILog Log { get; set; }

        /// <summary>
        /// Gets the name of the current method.
        /// </summary>
        /// <returns></returns>
        public static String GetCurrentMethodName()
        {
            return new StackFrame(1).GetMethod().Name;
        }

        /// <summary>
        /// Report current methodname to console.
        /// </summary>
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void ConsoleReport()
        {
            Console.WriteLine(new StackFrame(1).GetMethod().Name);
            try
            {
                if (Log != null && Log.LogLevel >= LogLevel.Debug)
                    Log.Debug(new StackFrame(1).GetMethod().Name);
            }
            catch (ObjectDisposedException ex)
            {
                Log = null;
                ConsoleReport("Error using debug log: {0}", ex.Message);
            }
        }

        /// <summary>
        /// Report current methodname and a value to Console.
        /// </summary>
        /// <param name="value">The value.</param>
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void ConsoleReport(Object value)
        {
            var methodName = new StackFrame(1).GetMethod().Name;
            var message = String.Format("{0} ({1})", methodName, value);
            Console.WriteLine(message);
            try
            {
                if (Log != null && Log.LogLevel >= LogLevel.Debug)
                    Log.Debug(message);
            }
            catch (ObjectDisposedException ex)
            {
                Log = null;
                ConsoleReport("Error using debug log: {0}", ex.Message);
            }
        }

        /// <summary>
        /// Report to the console.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="parameters">The parameters.</param>
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void ConsoleReport(String message, params object[] parameters)
        {
            var methodName = new StackFrame(1).GetMethod().Name;
            Console.WriteLine(String.Format("{0} {1}", methodName, String.Format(message, parameters)));
            try
            {
                if (Log != null && Log.LogLevel >= LogLevel.Debug)
                    Log.Debug(message, parameters);
            }
            catch (ObjectDisposedException ex)
            {
                Log = null;
                ConsoleReport("Error using debug log: {0}", ex.Message);
            }
        }
    }
}